﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Site_SetUp_NoAdmin : BaseSitePage
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        dsUsers.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.User>>(dsUsers_SelectData);
        grid.ItemCommand += new Telerik.Web.UI.GridCommandEventHandler(grid_ItemCommand);
        btnCreateAdmin.Click += new EventHandler(btnCreateAdmin_Click);
    }

    void btnCreateAdmin_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            Acms.Bll.User user = Acms.Bll.User.CreateInstance(AcmsContext.SiteId);
            user.FirstName = txtFirstName.Text.Trim();
            user.LastName = txtLastName.Text.Trim();
            user.EmailAddress = txtEmail.Text.Trim();
            user.UserName = txtUserName.Text.Trim();
            user.SetPassword(null, txtPassword.Text.Trim());
            user.BeginTransaction();

            try
            {
                user.Update();

                Acms.Bll.UserRole role = Acms.Bll.UserRole.CreateInstance(user, Acms.Bll.Role.Roles.Developer, AcmsContext.Site);
                role.UseTransaction(user.TransactionKey);
                role.Update();

                user.CommitTransaction();

                Response.Redirect("~/", true);
            }
            catch (Acms.Exceptions.DuplicateEmailException ex)
            {
                lblError.Visible = true;
                lblError.Text = ex.Message;
            }
            catch (Acms.Exceptions.DuplicateUsernameException ex)
            {
                lblError.Visible = true;
                lblError.Text = ex.Message;
            }
        }
    }

    void grid_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "SelectUser")
        {
            int userId = (int)grid.MasterTableView.DataKeyValues[e.Item.ItemIndex]["Id"];
            Acms.Bll.UserRole role = Acms.Bll.UserRole.CreateInstance(userId, (int)Acms.Bll.Role.Roles.Developer, AcmsContext.SiteId);
            role.Update();
            Response.Redirect("~/", true);
        }
    }

    protected List<Acms.Bll.User> GetUsers()
    {
        return Acms.Bll.User.GetGloabal();
    }

    void dsUsers_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.User> e)
    {
        e.SetData(GetUsers());
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (BasePage.HasAdmin && 1 == 2)
            {
                Response.Redirect("~/", true);
            }
            else if (Request.IsAuthenticated && 1==2)
            {
                Acms.Bll.UserRole role = Acms.Bll.UserRole.CreateInstance(User.Identity.User, Acms.Bll.Role.Roles.Developer, AcmsContext.Site);
                role.Update();
                Response.Redirect("~/", true);
            }
            else
            {
                DataBind();
            }
        }
    }

    public override Acms.Bll.Role.Roles[] AllowedRoles
    {
        get { return new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Anonomous, Acms.Bll.Role.Roles.Registered }; }
    }
}
